﻿Imports System.Collections.Specialized

Public Class frmTabLineage

    Dim EditNode As EditXML = New EditXML()
    Public Shared cMethods As Collection = New Collection()
    Public Shared cSources As Collection = New Collection()
    Public Shared cProcess As Collection = New Collection()
    Public Shared sPdelim As String = "£"

    Public Sub editXMLFile()
        EditNode.Remove("/metadata/dataqual/lineage", "method")
        EditNode.Remove("/metadata/dataqual/lineage", "srcinfo")
        EditNode.Remove("/metadata/dataqual/lineage", "procstep")

        'METHOD
        For i As Integer = cMethods.Count To 1 Step -1
            Dim item As MethodClass = cMethods(i)
            Dim newNode As String = "<methtype>" & item.MethodType & "</methtype>"
            For j As Integer = item.MethodID.Count To 1 Step -1
                Dim iditems() As String = item.MethodID(j - 1).Split(sPdelim)
                newNode = newNode & "<methodid><methkt>" & iditems(0) & "</methkt>"
                Dim keys() As String = iditems(1).Split(vbCrLf)
                For k As Integer = 1 To keys.Length
                    newNode = newNode & "<methkey>" & keys(k - 1).Trim() & "</methkey>"
                Next
                newNode = newNode & "</methodid>"
            Next
            newNode = newNode & "<methdesc>" & item.MethodDesc & "</methdesc>"
            For j As Integer = item.CiteCount To 1 Step -1
                newNode = newNode & "<methcite>"
                newNode = newNode & EditNode.createCiteInfo(item.FetchCite(j), item.FetchLWC_b(j), item.FetchLWC(j))
                newNode = newNode & "</methcite>"
            Next
            EditNode.Add("/metadata/dataqual/lineage", "method", "method", newNode)
        Next

        'SOURCE
        For i As Integer = cSources.Count To 1 Step -1
            Dim item As SourceClass = cSources(i)
            Dim newNode As String = ""
            'Cite Information
            newNode = newNode & "<srccite>"
            newNode = newNode & EditNode.createCiteInfo(item.SourceCite, item.boolLWC, item.SourceLWC)
            newNode = newNode & "</srccite>"
            newNode = newNode & "<srcscale>" & item.ScaleDenom & "</srcscale><typesrc>" & item.Media & "</typesrc>"
            'Source Time
            newNode = newNode & "<srctime>"
            If item.Gregorian Then
                newNode = newNode & EditNode.createTimeInfo(item.TimePeriod, item.boolSingle, item.boolRange)
            Else
                newNode = newNode & EditNode.createGeoTimeInfo(item.TimePeriod, item.boolSingle, item.boolRange)
            End If
            newNode = newNode & "<srccurr>" & item.Currentness & "</srccurr></srctime>"
            newNode = newNode & "<srccitea>" & item.CiteAbbrv & "</srccitea><srccontr>" & item.Contrib & "</srccontr>"

            EditNode.Add("/metadata/dataqual/lineage", "/method", "srcinfo", newNode)
        Next
        'PROCESS
        For i As Integer = cProcess.Count To 1 Step -1
            Dim item As ProcessClass = cProcess(i)
            Dim newNode As String = "<procdesc>" & item.Description & "</procdesc>"
            newNode = newNode & "<srcused>" & item.UsedAbbrv & "</srcused>"
            newNode = newNode & "<procdate>" & item.ProcessDate & "</procdate>"
            newNode = newNode & "<proctime>" & item.ProcessTime & "</proctime>"
            newNode = newNode & "<srcprod>" & item.ProducedAbbrv & "</srcprod>"
            If (item.ProcessContact.Person <> "" Or item.ProcessContact.Organization <> "") Then
                newNode = newNode & "<proccont>"
                newNode = newNode & EditNode.createContactInfo(item.ProcessContact)
                newNode = newNode & "</proccont>"
            End If
            EditNode.Add("/metadata/dataqual/lineage", "/srcinfo", "procstep", newNode)
        Next
    End Sub

    Private Sub btnAddMeth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMeth.Click
        Dim oNewMethod As New MethodClass ' create new Method object
        Dim sMethDisplay As String = ""
        Dim frmNewMethod As New frmMethod(oNewMethod, "N")
        frmNewMethod.ShowDialog()
        If frmNewMethod.getUpdated() Then 'add the new methodology
            oNewMethod = frmNewMethod.getMethod()
            cMethods.Add(oNewMethod)
            sMethDisplay = oNewMethod.MethodType & ":  " & Microsoft.VisualBasic.Left(oNewMethod.MethodDesc, 60)
            lboxMeth.Items.Add(sMethDisplay)
            editXMLFile()
        End If

    End Sub

    Private Sub btnEditMeth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditMeth.Click
        Dim iCount As Integer = lboxMeth.Items.Count
        Dim iSelInd As Integer = lboxMeth.SelectedIndex 'get selected methodology to edit
        If (iCount = 1) Then iSelInd = 0 'default to edit unique entry if only one present
        If (iSelInd = -1) Then
            MessageBox.Show("No Methodology selected for editing!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim oNewMethod As MethodClass = CType(cMethods(iSelInd + 1), MethodClass) 'retrieve Method object
        Dim sMethDisplay As String = ""
        Dim frmEditMethod As New frmMethod(oNewMethod, "E")
        frmEditMethod.ShowDialog()
        If frmEditMethod.getUpdated() Then 'update the methodology
            oNewMethod = frmEditMethod.getMethod()
            cMethods.Add(oNewMethod, , iSelInd + 1) '1-based index
            cMethods.Remove(iSelInd + 2) '1-based index
            sMethDisplay = oNewMethod.MethodType & ":  " & Microsoft.VisualBasic.Left(oNewMethod.MethodDesc, 60)
            lboxMeth.Items.RemoveAt(iSelInd)
            lboxMeth.Items.Insert(iSelInd, sMethDisplay)
            editXMLFile()
        End If
        btnEditMeth.Enabled = False
        btnDelMeth.Enabled = False
    End Sub

    Private Sub btnDelMeth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelMeth.Click
        Dim iSelInd As Integer = lboxMeth.SelectedIndex 'get selected methodology to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Methodology selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        cMethods.Remove(iSelInd + 1) 'update collection, 1-based index
        lboxMeth.Items.RemoveAt(iSelInd) 'update list box, 0-based index
        editXMLFile()
        btnEditMeth.Enabled = False
        btnDelMeth.Enabled = False
    End Sub

    Private Sub btnAddSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSource.Click
        Dim oNewSource As New SourceClass ' create new Source object
        Dim s1 As String = ""
        Dim frmNewSource As New frmSourceInfo(oNewSource)
        frmNewSource.ShowDialog()
        If frmNewSource.getUpdated() Then 'add the new source info
            oNewSource = frmNewSource.getSource()
            cSources.Add(oNewSource)
            s1 = oNewSource.CiteAbbrv & ":  " & Microsoft.VisualBasic.Left(oNewSource.Contrib, 40)
            lboxLinSource.Items.Add(s1)
            editXMLFile()
        End If

    End Sub

    Private Sub btnEditSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditSource.Click
        Dim iCount As Integer = lboxLinSource.Items.Count
        Dim iSelInd As Integer = lboxLinSource.SelectedIndex 'get selected source information to edit
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No Source selected for editing!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        iSelInd = iSelInd + 1 'adjust to 1-based index scale
        Dim oEditSource As SourceClass = CType(cSources(iSelInd), SourceClass) 'retrieve Source object, 1-based index
        Dim s1 As String = ""
        Dim frmEditSource As New frmSourceInfo(oEditSource)
        frmEditSource.ShowDialog()
        If frmEditSource.getUpdated() Then 'add the new source info
            oEditSource = frmEditSource.getSource()
            cSources.Add(oEditSource, , iSelInd)
            cSources.Remove(iSelInd + 1)
            s1 = oEditSource.CiteAbbrv & ":  " & Microsoft.VisualBasic.Left(oEditSource.Contrib, 40)
            lboxLinSource.Items.Insert(iSelInd - 1, s1) '0-based index
            lboxLinSource.Items.RemoveAt(iSelInd)
            editXMLFile()
        End If
        btnEditSource.Enabled = False
        btnDelSource.Enabled = False
    End Sub

    Private Sub btnDelSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelSource.Click
        Dim iSelInd As Integer = lboxLinSource.SelectedIndex 'get selected source information to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Source selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        cSources.Remove(iSelInd + 1) 'update collection, 1-based index
        lboxLinSource.Items.RemoveAt(iSelInd) 'update list box, 0-based index
        editXMLFile()
        btnEditSource.Enabled = False
        btnDelSource.Enabled = False
    End Sub

    Private Sub btnAddProc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddProc.Click
        Dim oNewProc As New ProcessClass ' create new Process Step object
        Dim s1 As String = ""
        Dim frmNewProc As New frmProcessStep(oNewProc)
        frmNewProc.ShowDialog()
        If frmNewProc.getUpdated() Then 'add the new process step info
            oNewProc = frmNewProc.getProcess()
            cProcess.Add(oNewProc)
            s1 = Microsoft.VisualBasic.Left(oNewProc.Description, 60)
            lboxProcess.Items.Add(s1)
            editXMLFile()
        End If

    End Sub

    Private Sub btnEditProc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditProc.Click
        Dim iCount As Integer = lboxProcess.Items.Count
        Dim iSelInd As Integer = lboxProcess.SelectedIndex 'get selected process step to edit
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No Process Step selected for editing!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        iSelInd = iSelInd + 1 'adjust to 1-based index scale
        Dim oEditProc As ProcessClass = CType(cProcess(iSelInd), ProcessClass) 'retrieve Process Step object, 1-based index
        Dim s1 As String = ""
        Dim frmEditProc As New frmProcessStep(oEditProc)
        frmEditProc.ShowDialog()
        If frmEditProc.getUpdated() Then 'add the new source info
            oEditProc = frmEditProc.getProcess()
            cProcess.Add(oEditProc, , iSelInd)
            cProcess.Remove(iSelInd + 1)
            s1 = Microsoft.VisualBasic.Left(oEditProc.Description, 60)
            lboxProcess.Items.Insert(iSelInd - 1, s1) '0-based index
            lboxProcess.Items.RemoveAt(iSelInd)
            editXMLFile()
        End If
        btnEditProc.Enabled = False
        btnDelProc.Enabled = False
    End Sub

    Private Sub btnDelProc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelProc.Click
        Dim iSelInd As Integer = lboxProcess.SelectedIndex 'get selected process step to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Process Step selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        cProcess.Remove(iSelInd + 1) 'update collection, 1-based index
        lboxProcess.Items.RemoveAt(iSelInd) 'update list box, 0-based index
        editXMLFile()
        btnEditProc.Enabled = False
        btnDelProc.Enabled = False
    End Sub

    Private Sub lboxMeth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxMeth.SelectedIndexChanged
        If lboxMeth.SelectedItems.Count > 0 Then btnEditMeth.Enabled = True : btnDelMeth.Enabled = True Else btnEditMeth.Enabled = False : btnDelMeth.Enabled = False
    End Sub

    Private Sub lboxLinSource_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxLinSource.SelectedIndexChanged
        If lboxLinSource.SelectedItems.Count > 0 Then btnEditSource.Enabled = True : btnDelSource.Enabled = True Else btnEditSource.Enabled = False : btnDelSource.Enabled = False
    End Sub

    Private Sub lboxProcess_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxProcess.SelectedIndexChanged
        If lboxProcess.SelectedItems.Count > 0 Then btnEditProc.Enabled = True : btnDelProc.Enabled = True Else btnEditProc.Enabled = False : btnDelProc.Enabled = False
    End Sub

    Private Sub frmTabLineage_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("Lineage")
        helpForm.ShowDialog()
    End Sub
End Class